
/**
 * Description 解析属性并绑定当前上下文
 * @date 7/5/2022 - 4:49:23 PM
 * @author 孙畅
 *
 * @export
 * @param {*} $scope 上下文变量
 * @param {*} props 属性变量
 * @returns {{ [key: string]: any; }}
 */
export default function getProps($scope: any, props: any) {
  if (!(props && typeof props === 'object')) return {};
  
  const newProps: { [key: string]: any } = Array.isArray(props) ? [] : {};
  // 遍历 处理数据
  Object.keys(props).forEach((key) => {
    let val = props[key];
    if (typeof val === 'object') {
      // 嵌套属性, 深入解析
      newProps[key] = getProps($scope, val);
    }
    else if (!/^\{[\s\S]*\}$/.test(val) && !/^\[[\s\S]*\]$/.test(val)) {
      newProps[key] = val;
    } else {
      try {
        const objVal: { type: string; value: string } = JSON.parse(val);
        console.log('解析属性值 ===> ' + key, objVal, $scope);
        // 解析
        switch (objVal.type) {
          case 'string':
            newProps[key] = objVal.value;
            break;
          case 'express':
            try {
              newProps[key] = eval(objVal.value);
            } catch (error) {}
            break;
          case 'function':
            var newFunc = new Function(objVal.value);
            newProps[key] = newFunc($scope);
            break;
        }
      } catch (error) {
        newProps[key] = val;
      }
    }
  });
  console.log('处理后的newProps: ', newProps);
  return newProps;
}
